//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package gnu.io;

import gnu.util.DLLPath;

import java.io.FileDescriptor;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Vector;

public class CommPortIdentifier {
    public static final int PORT_SERIAL = 1;
    public static final int PORT_PARALLEL = 2;
    public static final int PORT_I2C = 3;
    public static final int PORT_RS485 = 4;
    public static final int PORT_RAW = 5;
    private String PortName;
    private boolean Available = true;
    private String Owner;
    private CommPort commport;
    private CommDriver RXTXDriver;
    static CommPortIdentifier CommPortIndex;
    CommPortIdentifier next;
    private int PortType;
    private static final boolean debug = false;
    static Object Sync = new Object();
    Vector ownershipListener;
    private boolean HideOwnerEvents;

    CommPortIdentifier(String var1, CommPort var2, int var3, CommDriver var4) {
        this.PortName = var1;
        this.commport = var2;
        this.PortType = var3;
        this.next = null;
        this.RXTXDriver = var4;
    }

    public static void addPortName(String var0, int var1, CommDriver var2) {
        AddIdentifierToList(new CommPortIdentifier(var0, (CommPort)null, var1, var2));
    }

    private static void AddIdentifierToList(CommPortIdentifier var0) {
        Object var1 = Sync;
        synchronized(Sync) {
            if(CommPortIndex == null) {
                CommPortIndex = var0;
            } else {
                CommPortIdentifier var2;
                for(var2 = CommPortIndex; var2.next != null; var2 = var2.next) {
                    ;
                }

                var2.next = var0;
            }

        }
    }

    public void addPortOwnershipListener(CommPortOwnershipListener var1) {
        if(this.ownershipListener == null) {
            this.ownershipListener = new Vector();
        }

        if(!this.ownershipListener.contains(var1)) {
            this.ownershipListener.addElement(var1);
        }

    }

    public String getCurrentOwner() {
        return this.Owner;
    }

    public String getName() {
        return this.PortName;
    }

    public static CommPortIdentifier getPortIdentifier(String var0) throws NoSuchPortException {
        Object var2 = Sync;
        CommPortIdentifier var1;
        synchronized(Sync) {
            for(var1 = CommPortIndex; var1 != null && !var1.PortName.equals(var0); var1 = var1.next) {
                ;
            }

            if(var1 == null) {
                getPortIdentifiers();

                for(var1 = CommPortIndex; var1 != null && !var1.PortName.equals(var0); var1 = var1.next) {
                    ;
                }
            }
        }

        if(var1 != null) {
            return var1;
        } else {
            throw new NoSuchPortException();
        }
    }

    public static CommPortIdentifier getPortIdentifier(CommPort var0) throws NoSuchPortException {
        Object var2 = Sync;
        CommPortIdentifier var1;
        synchronized(Sync) {
            var1 = CommPortIndex;

            while(true) {
                if(var1 == null || var1.commport == var0) {
                    break;
                }

                var1 = var1.next;
            }
        }

        if(var1 != null) {
            return var1;
        } else {
            throw new NoSuchPortException();
        }
    }

    public static Enumeration getPortIdentifiers() {
        Object var0 = Sync;
        synchronized(Sync) {
            HashMap var1 = new HashMap();

            for(CommPortIdentifier var2 = CommPortIndex; var2 != null; var2 = var2.next) {
                var1.put(var2.PortName, var2);
            }

            CommPortIndex = null;

            try {
                CommDriver var3 = (CommDriver)Class.forName("gnu.io.RXTXCommDriver").newInstance();
                var3.initialize();
                CommPortIdentifier var4 = CommPortIndex;

                for(CommPortIdentifier var5 = null; var4 != null; var4 = var4.next) {
                    CommPortIdentifier var6 = (CommPortIdentifier)var1.get(var4.PortName);
                    if(var6 != null && var6.PortType == var4.PortType) {
                        var6.RXTXDriver = var4.RXTXDriver;
                        var6.next = var4.next;
                        if(var5 == null) {
                            CommPortIndex = var6;
                        } else {
                            var5.next = var6;
                        }

                        var5 = var6;
                    } else {
                        var5 = var4;
                    }
                }
            } catch (Throwable var8) {
                System.err.println(var8 + " thrown while loading " + "gnu.io.RXTXCommDriver");
                System.err.flush();
            }
        }

        return new CommPortEnumerator();
    }

    public int getPortType() {
        return this.PortType;
    }

    public synchronized boolean isCurrentlyOwned() {
        return !this.Available;
    }

    public synchronized CommPort open(FileDescriptor var1) throws UnsupportedCommOperationException {
        throw new UnsupportedCommOperationException();
    }

    private native String native_psmisc_report_owner(String var1);

    public CommPort open(String var1, int var2) throws PortInUseException {
        boolean var3;
        synchronized(this) {
            var3 = this.Available;
            if(var3) {
                this.Available = false;
                this.Owner = var1;
            }
        }

        if(!var3) {
            long var4 = System.currentTimeMillis() + (long)var2;
            this.fireOwnershipEvent(3);
            synchronized(this) {
                long var6;
                while(!this.Available && (var6 = System.currentTimeMillis()) < var4) {
                    try {
                        this.wait(var4 - var6);
                    } catch (InterruptedException var25) {
                        Thread.currentThread().interrupt();
                        break;
                    }
                }

                var3 = this.Available;
                if(var3) {
                    this.Available = false;
                    this.Owner = var1;
                }
            }
        }

        if(!var3) {
            throw new PortInUseException(this.getCurrentOwner());
        } else {
            boolean var21 = false;

            CommPort var28;
            try {
                var21 = true;
                if(this.commport == null) {
                    this.commport = this.RXTXDriver.getCommPort(this.PortName, this.PortType);
                }

                if(this.commport == null) {
                    throw new PortInUseException(this.native_psmisc_report_owner(this.PortName));
                }

                this.fireOwnershipEvent(1);
                var28 = this.commport;
                var21 = false;
            } finally {
                if(var21) {
                    if(this.commport == null) {
                        synchronized(this) {
                            this.Available = true;
                            this.Owner = null;
                        }
                    }

                }
            }

            if(this.commport == null) {
                synchronized(this) {
                    this.Available = true;
                    this.Owner = null;
                }
            }

            return var28;
        }
    }

    public void removePortOwnershipListener(CommPortOwnershipListener var1) {
        if(this.ownershipListener != null) {
            this.ownershipListener.removeElement(var1);
        }

    }

    void internalClosePort() {
        synchronized(this) {
            this.Owner = null;
            this.Available = true;
            this.commport = null;
            this.notifyAll();
        }

        this.fireOwnershipEvent(2);
    }

    void fireOwnershipEvent(int var1) {
        if(this.ownershipListener != null) {
            Enumeration var3 = this.ownershipListener.elements();

            while(var3.hasMoreElements()) {
                CommPortOwnershipListener var2 = (CommPortOwnershipListener)var3.nextElement();
                var2.ownershipChange(var1);
            }
        }

    }

    static {
        try {
            CommDriver var0 = (CommDriver)Class.forName("gnu.io.RXTXCommDriver").newInstance();
            var0.initialize();
        } catch (Throwable var1) {
            System.err.println(var1 + " thrown while loading " + "gnu.io.RXTXCommDriver");
        }

        String var2 = System.getProperty("os.name");
        if(var2.toLowerCase().indexOf("linux") == -1) {
            ;
        }
        System.load(DLLPath.getDllPath("rxtxSerial.dll"));
    }
}
